//
//  TKIMFriendBaseListView.m
//  TKIM_首创
//
//  Created by liupm on 15/12/11.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMFriendBaseListView.h"
#import "TKIMLayerUtils.h"
#import "TKIMHelper.h"

@interface TKIMFriendBaseListView()
//好友列表更新队列
@property(nonatomic,strong) dispatch_queue_t friendQueue;
@end

@implementation TKIMFriendBaseListView

-(instancetype)initWithFrame:(CGRect)frame widthDelegate:(id)delegate
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _friendQueue = dispatch_queue_create("friendQueue", nil);
        _delegate = delegate;
        _dataList = [NSMutableArray array];
        _sectionTitle = [NSMutableArray array];
        _setGroup =  [[TKIMSettingGroup alloc] initWithHeaderTitle:nil footerTitle:nil settingItems: nil];
        [self drawView];
    }
    return self;
}


/**
 *  @Author lpm, 15-10-28 17:10:29
 *
 *  @brief  绘制界面
 */
-(void)drawView
{
    //列表
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, self.frame.size.width, self.frame.size.height-108) style:UITableViewStylePlain];
    [_tableView setTableFooterView:[UIView new]];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 55;
    _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _tableView.separatorColor = [TKIMColorHelper stringToColor:@"#DCDCDC"];
    _tableView.backgroundColor = [TKIMColorHelper stringToColor:@"#EFEFF4"];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _footLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width, 50)];
    _footLabel.backgroundColor = [UIColor clearColor];
    _footLabel.textAlignment = NSTextAlignmentCenter;
    _footLabel.textColor = [TKIMColorHelper stringToColor:@"#616264"];
    _footLabel.font = [UIFont systemFontOfSize:15.0f];
    [_tableView setTableFooterView:_footLabel];
    
    [_tableView setSectionIndexBackgroundColor:[UIColor clearColor]];
    [_tableView setSectionIndexColor:[UIColor grayColor]];
    
    [self addSubview:self.searchBar];
    [self addSubview:_tableView];
    
//    [self searchController];
}


- (UISearchBar *)searchBar
{
    if (!_searchBar)
    {
        _searchBar = [[TKIMSearchBar alloc] initWithFrame: CGRectMake(0, 0, self.frame.size.width, 44)];
        _searchBar.delegate = self;
        _searchBar.placeholder = @"搜索";
        _searchBar.backgroundColor = [TKIMColorHelper stringToColor:@"#EFEFF4"];
        [_searchBar setTextFieldBackgroudColor:[UIColor whiteColor]];
        [_searchBar setCancelReturnKeyType: UIReturnKeyDone];
    }
    return _searchBar;
}

/**
 *  @author zhengrs, 16-01-11 13:01:27
 *
 *  @brief 隐藏搜索框
 */
-(void)hideSearchBar
{
    [self.searchBar removeFromSuperview];
    self.tableView.frame = CGRectMake(0, 0, self.width, self.height-64);
}

#pragma mark - UISearchBarDelegate



/**
 *  @Author lpm, 15-10-29 11:10:03
 *
 *  @brief  刷新界面
 */
-(void)reloadData:(NSMutableArray *)dataArray
{
    if (dataArray)
    {
        __weak typeof(self) weakSelf = self;
        dispatch_barrier_async(_friendQueue, ^ {
            weakSelf.dataList = [TKIMHelper getFriendListDataBy:dataArray];
            weakSelf.sectionTitle = [TKIMHelper getFriendListSectionBy:weakSelf.dataList];
            if(weakSelf.sectionTitle && weakSelf.sectionTitle.count >0)
            {
                dispatch_barrier_async(dispatch_get_main_queue(), ^ {
                    [weakSelf.tableView reloadData];
                });
            }
       });
    }
    else
    {
        [_tableView reloadData];
    }
}


#pragma mark - Table view data source
/**
 *  @Author lpm, 15-10-21 14:10:35
 *
 *  @brief
 *
 *  @param tableView
 *
 *  @return
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataList.count+1;
}


/**
 *  @Author lpm, 15-10-21 14:10:42
 *
 *  @brief
 *
 *  @param tableView
 *  @param section
 *
 *  @return
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        return _setGroup.itemsCount;
    }
    NSArray *array = (NSArray *)_dataList[section-1];
    return array.count;
}


/**
 *  @Author lpm, 15-10-21 14:10:49
 *
 *  @brief
 *
 *  @param tableView
 *  @param indexPath
 *
 *  @return
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}


/**
 *  @Author lpm, 15-10-29 12:10:24
 *
 *  @brief
 *
 *  @param tableView
 *  @param section
 *
 *  @return
 */
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 0;
    }
    return 20;
}


/**
 *  @Author lpm, 15-10-29 12:10:54
 *
 *  @brief
 *
 *  @param tableView
 *  @param section
 *
 *  @return
 */
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return nil;
    }
    
    id label = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
    if (label == nil) {
        label = [[UILabel alloc] init];
        [label setFont:[UIFont systemFontOfSize:15.0f]];
        [label setBackgroundColor:[TKIMColorHelper stringToColor:@"#EFEFF4"]];
    }
    NSInteger count = _sectionTitle.count -1;
    if(count >= section)
    {
        [label setText:[NSString stringWithFormat:@"  %@", [_sectionTitle objectAtIndex:section]]];
    }
    return label;
}


/**
 *  @Author lpm, 15-10-29 12:10:23
 *
 *  @brief
 *
 *  @param tableView
 *
 *  @return
 */
- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return _sectionTitle;
}


/**
 *  @Author lpm, 15-10-29 12:10:15
 *
 *  @brief
 *
 *  @param tableView
 *  @param title
 *  @param index
 *
 *  @return
 */
- (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    if(index == 0) {
//        [self.tableView scrollRectToVisible:_searchController.searchBar.frame animated:NO];
        return -1;
    }
    else {
        return index - 1;
    }
}


/**
 *  @Author lpm, 15-12-11 14:12:33
 *
 *  @brief
 *
 *  @param tableView
 *  @param indexPath
 */
-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section !=0)
    {
        NSArray *list = _dataList[indexPath.section-1];
        _selectUser = list[indexPath.row];
    }
}

-(void)dealloc
{
    _friendQueue = nil;
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
}


@end
